package com.dbdata.move.service;

import com.dbdata.move.PinYinTool;
import com.dbdata.move.Utils;
import com.dbdata.move.dao.UserDao;
import com.dbdata.move.model.UserBaseInfo;
import com.wksc.framework.dao.BaseDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author nibo
 */
@Service
public class UserService extends BaseDao{

    private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserDao dao;

    /**
     * 根据用户身份证号或护照号更新用户的组织机构
     */
    public void updateUserOrg() {
        // 遂宁分院
        int organizeId = 6;
        String filePath = "F:\\backupfile\\stu.txt";
        List<String> list = new ArrayList<String>();
        try {
            list = Utils.readCsvFileNoHead(filePath, "GBK");
        } catch (IOException e) {
            LOGGER.error("读取学员原始数据出错！", e);
        }

        for (String s : list) {
            String[] item = s.trim().split(",", -1);
            LOGGER.info("===开始更新用户：{}(身份证或护照:{})，组织机构(机构id:{})信息===",
                    item[1].trim(), item[2].trim(), organizeId);
            if (StringUtils.isEmpty(item[2])) {
                LOGGER.warn("用户：{}，身份证或护照为空！！，跳过！！");
                continue;
            }
            dao.updateUserOrganize(item[2].trim(), organizeId);
        }
    }

    /**
     * 更新所有用户的拼音简写
     */
    public void updateUserPinYinSX() throws SQLException {
        dao.updateUserPinYinSX();
    }

}
